package com.ruoyi.erecord.locus.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.tools.enums.DeviceTypeEnum;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.erecord.locus.domain.Locus;
import com.ruoyi.erecord.locus.domain.bo.LocusBo;
import com.ruoyi.erecord.locus.domain.vo.LocusVo;
import com.ruoyi.erecord.locus.mapper.LocusMapper;
import com.ruoyi.erecord.locus.service.ILocusService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 轨迹Service业务层处理
 *
 * @author cc
 * @date 2023-05-01
 */
@RequiredArgsConstructor
@Service
public class LocusServiceImpl implements ILocusService {

    private final LocusMapper baseMapper;

    /**
     * 查询轨迹
     */
    @Override
    public LocusVo queryById(String id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询轨迹列表
     */
    @Override
    public TableDataInfo<LocusVo> queryPageList(LocusBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Locus> lqw = buildQueryWrapper(bo);
        Page<LocusVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询轨迹列表
     */
    @Override
    public List<LocusVo> queryList(LocusBo bo) {
        LambdaQueryWrapper<Locus> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Locus> buildQueryWrapper(LocusBo bo) {
        Map<String, Object> params = bo.getParams();

        LambdaQueryWrapper<Locus> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceType()), Locus::getDeviceType, bo.getDeviceType());
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceOriginalId()), Locus::getDeviceOriginalId, bo.getDeviceOriginalId());

        lqw.like(StringUtils.isNotBlank(bo.getDeviceName()), Locus::getDeviceName, bo.getDeviceName());
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceCode()), Locus::getDeviceCode, bo.getDeviceCode());
        lqw.between(params.get("beginGpsTime") != null && params.get("endGpsTime") != null,
            Locus::getGpsTime, params.get("beginGpsTime"), params.get("endGpsTime"));

        //lqw.orderByDesc(Locus::getGpsTime);
        return lqw;
    }

    /**
     * 新增轨迹
     */
    @Override
    public Boolean insertByBo(LocusBo bo) {
        Locus add = BeanUtil.toBean(bo, Locus.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            // bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改轨迹
     */
    @Override
    public Boolean updateByBo(LocusBo bo) {
        Locus update = BeanUtil.toBean(bo, Locus.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Locus entity) {
        // TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除轨迹
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if (isValid) {
            // TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public LocusVo getLocusVoByOrgId(DeviceTypeEnum deviceType, Long orgId) {
        LambdaQueryWrapper<Locus> lqw = Wrappers.lambdaQuery();
        lqw.eq(Locus::getDeviceType, deviceType.getCode());
        lqw.eq(Locus::getDeviceOriginalId, orgId);
        lqw.orderBy(true, false, Locus::getId);
        lqw.last("limit 1");
        return this.baseMapper.selectVoOne(lqw);
    }
}
